package pt.isel.lercm.projectmanager.managedController;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import pt.isel.lercm.projectmanager.business.EmailBusiness;
import pt.isel.lercm.projectmanager.managedController.generic.GenericManagedBean;
import pt.isel.lercm.projectmanager.spring.model.AnoLectivo;
import pt.isel.lercm.projectmanager.spring.model.User;
import pt.isel.lercm.projectmanager.spring.service.IAlunoService;
import pt.isel.lercm.projectmanager.spring.service.IAnoLectivoService;
import pt.isel.lercm.projectmanager.spring.service.IConstanteService;
import pt.isel.lercm.projectmanager.spring.service.IEmailTemplateService;
import pt.isel.lercm.projectmanager.spring.service.IGrupoGithubService;
import pt.isel.lercm.projectmanager.spring.service.IGrupoService;
import pt.isel.lercm.projectmanager.spring.service.IUserService;

@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean extends GenericManagedBean implements Serializable{
	
	private static final long serialVersionUID = -556259271208514650L;

	private String username;
	private String password;
	
	private boolean loggedIn;
	
	private String lastPath;
	
	private String anoLectivo;
	
	@ManagedProperty(value="#{UserService}")
	IUserService userService;
	
	@ManagedProperty(value="#{AlunoService}")
	IAlunoService alunoService;

	@ManagedProperty(value="#{GrupoService}")
	IGrupoService grupoService;
	
	@ManagedProperty(value="#{EmailTemplateService}")
	IEmailTemplateService emailTemplateService;
	
	@ManagedProperty(value="#{GrupoGithubService}")
	IGrupoGithubService grupoGithubService;
	
	@ManagedProperty(value="#{ConstanteService}")
	IConstanteService constanteService;
	
	@ManagedProperty(value="#{AnoLectivoService}")
	IAnoLectivoService anoLectivoService;
	
	User user;
	
	EmailBusiness emailBusiness;
	
	public IAlunoService getAlunoService() {
		return alunoService;
	}

	public void setAlunoService(IAlunoService alunoService) {
		this.alunoService = alunoService;
	}

	public IGrupoService getGrupoService() {
		return grupoService;
	}

	public void setGrupoService(IGrupoService grupoService) {
		this.grupoService = grupoService;
	}

	public IEmailTemplateService getEmailTemplateService() {
		return emailTemplateService;
	}

	public void setEmailTemplateService(IEmailTemplateService emailTemplateService) {
		this.emailTemplateService = emailTemplateService;
	}

	public EmailBusiness getEmailBusiness() {
		return emailBusiness;
	}

	public void setEmailBusiness(EmailBusiness emailBusiness) {
		this.emailBusiness = emailBusiness;
	}
	
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	private void getUserDb(){
		user = userService.getById(1);
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}	
	
	public void login() {  

		getUserDb();
		
		if(username != null  && username.equals(user.getUsername()) && password != null  && password.equals(user.getPassword())) {
			loggedIn = true;
			
			if (lastPath != null){
				redirectToPage(lastPath, false);
			}
			else{
				redirectToPage("/app/home.xhtml", false);
			}
			
			//return "home";
        }
		else{
			FacesContext.getCurrentInstance().addMessage(null, 
       			 new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Invalid Login!",
                    "Please Try Again!"));
		
			redirectToPage("/login/login.xhtml", true);
			//return "login";
		} 
    }
	
	public void doLogout() {
		loggedIn = false;
		lastPath=null;
		
        FacesMessage msg = new FacesMessage("Logout success!", "INFO MSG");
        msg.setSeverity(FacesMessage.SEVERITY_INFO);
        FacesContext.getCurrentInstance().addMessage(null, msg);
         
        redirectToPage("/login/login.xhtml", false);
    }
	
	public void checkEmail(){
		
		emailBusiness = new EmailBusiness(alunoService,grupoService,emailTemplateService,grupoGithubService,constanteService,anoLectivoService);
		
		List<FacesMessage> mensagens = emailBusiness.checkEmail(false, false);
		
		for(FacesMessage msg : mensagens){
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
		return;
		
	}
	
	public IGrupoGithubService getGrupoGithubService() {
		return grupoGithubService;
	}

	public void setGrupoGithubService(IGrupoGithubService grupoGithubService) {
		this.grupoGithubService = grupoGithubService;
	}

	public IConstanteService getConstanteService() {
		return constanteService;
	}

	public void setConstanteService(IConstanteService constanteService) {
		this.constanteService = constanteService;
	}

	public IAnoLectivoService getAnoLectivoService() {
		return anoLectivoService;
	}

	public void setAnoLectivoService(IAnoLectivoService anoLectivoService) {
		this.anoLectivoService = anoLectivoService;
	}

	public boolean isLoggedIn() {
		return loggedIn;
	}

	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

	@Override
	public void init() {
		
	}

	public String getLastPath() {
		return lastPath;
	}

	public void setLastPath(String lastPath) {
		this.lastPath = lastPath;
	}

	public String getAnoLectivo() {
		if (anoLectivo == null){
			String idAno = constanteService.getValorByName("ANO_ACTUAL");
			
			AnoLectivo ano =  anoLectivoService.getById(Integer.parseInt(idAno));
			
			anoLectivo = ano.getDescricao();
		}
		
		return anoLectivo;
	}

	public void setAnoLectivo(String anoLectivo) {
		this.anoLectivo = anoLectivo;
	}

}
